package com.lv.suyiyong.hfr.adapter

import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lv.common.base.DataBindBaseViewHolder
import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.LoadSrc
import com.lv.common.ktx.setBname
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.Constants
import com.lv.common.utils.YgxUtilsKt
import com.lv.service.room.company.CompanyItemBean
import com.lv.service.utils.UserUtils
import com.lv.service.utils.YgxToolsKt
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.ItemFactoryLocationTabBinding


/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class LocationFactoryTabAdapter(
    private val datas: MutableList<CompanyItemBean>,
) :
    BaseQuickAdapter<CompanyItemBean, DataBindBaseViewHolder>
        (R.layout.item_factory_location_tab, datas) {

    override fun convert(holder: DataBindBaseViewHolder, item: CompanyItemBean) {
        val mBinding: ItemFactoryLocationTabBinding =
            holder.getBind() as ItemFactoryLocationTabBinding
//        mBinding.itemRoot = item
//        //防止列表闪烁
        mBinding.executePendingBindings()
        val layoutPosition = holder.layoutPosition
        val isspecially = item.isspecially
        if (isspecially == "2") {
            mBinding.ivGold.visibility = View.VISIBLE
            mBinding.ivGold.LoadSrc(Constants.GOLD_TAG)
        } else {
            mBinding.ivGold.visibility = View.GONE
        }
        val spuList = item.spuList
        if (spuList.isNullOrEmpty()) {
            mBinding.ivImg1.visibility = View.GONE
            mBinding.ivImg2.visibility = View.GONE
            mBinding.ivImg3.visibility = View.GONE
        } else if (spuList.size == 1) {
            mBinding.ivImg1.visibility = View.VISIBLE
            mBinding.ivImg1.LoadSrc(spuList[0].image)
            mBinding.ivImg2.visibility = View.GONE
            mBinding.ivImg3.visibility = View.GONE
        } else if (spuList.size == 2) {
            mBinding.ivImg1.visibility = View.VISIBLE
            mBinding.ivImg2.visibility = View.VISIBLE
            mBinding.ivImg1.LoadSrc(spuList[0].image)
            mBinding.ivImg2.LoadSrc(spuList[1].image)
            mBinding.ivImg3.visibility = View.GONE
        } else if (spuList.size >= 2) {
            mBinding.ivImg1.visibility = View.VISIBLE
            mBinding.ivImg2.visibility = View.VISIBLE
            mBinding.ivImg3.visibility = View.VISIBLE
            mBinding.ivImg1.LoadSrc(spuList[0].image)
            mBinding.ivImg2.LoadSrc(spuList[1].image)
            mBinding.ivImg3.LoadSrc(spuList[2].image)
        }
        item.merito?.setBname(sub = "", tvBname = mBinding.tvBname, length = 2)
        mBinding.tvName.text = item.name
        mBinding.tvAddress.text = "${item.province}${item.city}"
        mBinding.clParent.setOnClickListener {
            onViewClickListener?.onClick(layoutPosition, item)
            ARouter.getInstance().build(ARoutUtils.CompanyInfoActivity).withString("cpyid", item.id)
                .navigation()
        }
        mBinding.ivImg1.setOnClickListener {
            val id = spuList?.get(0)?.id
            ARouter.getInstance().build(ARoutUtils.GoodsDetilsActivity).withString("spuid", id)
                .navigation()
        }
        mBinding.ivImg2.setOnClickListener {
            val id = spuList?.get(1)?.id
            ARouter.getInstance().build(ARoutUtils.GoodsDetilsActivity).withString("spuid", id)
                .navigation()
        }
        mBinding.ivImg3.setOnClickListener {
            val id = spuList?.get(2)?.id
            ARouter.getInstance().build(ARoutUtils.GoodsDetilsActivity).withString("spuid", id)
                .navigation()
        }
        mBinding.tvPhone.setOnClickListener {
            val contact = item.contact
            if (!contact.isNullOrEmpty()) {
                YgxUtilsKt.callPhone(context, contact[0].tel ?: "")
            }
        }
        mBinding.vChat.setOnClickListener {
            onViewChatClickListener?.onClick(layoutPosition,item)
        }

    }

    var onViewClickListener: OnPositionTListener<CompanyItemBean>? = null
    var onViewChatClickListener: OnPositionTListener<CompanyItemBean>? = null


}